package com.alibaba.otter.manager.web.resource;

import com.alibaba.otter.manager.biz.dto.CommonSearchDTO;
import com.alibaba.otter.manager.biz.config.canal.CanalService;
import com.alibaba.otter.manager.web.dto.CanalDTO;
import com.alibaba.otter.manager.web.dto.RestResponse;
import com.alibaba.otter.manager.web.service.CanalAggService;
import com.alibaba.otter.manager.web.utils.PaginationUtil;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * canal 配置
 */
@RequestMapping("/api/v1/canal")
@RestController
public class CanalResource {

    private final CanalService canalService;


    private final CanalAggService canalAggService;

    public CanalResource(CanalService canalService, CanalAggService canalAggService) {
        this.canalService = canalService;
        this.canalAggService = canalAggService;
    }


    /**
     * 查询canal节点详情信息
     */
    @GetMapping("findOne/{id}")
    public RestResponse<CanalDTO> findOne(@PathVariable("id") Long id){
        return RestResponse.ok(canalAggService.findOne(id));
    }


    /**
     * 查询列表信息
     */
    @GetMapping("list")
    public RestResponse<List<CanalDTO>> list(CommonSearchDTO searchVo, Pageable pageable){
        return PaginationUtil.pageHttpResponse(canalAggService.list(searchVo, pageable));
    }


    /**
     * 删除canal配置
     */
    @DeleteMapping("delete/{id}")
    public RestResponse<Void> delete(@PathVariable("id") Long id){
        canalService.remove(id);
        return RestResponse.ok();
    }


    /**
     * 添加canal
     */
    @PostMapping("create")
    public RestResponse<Void> create(@RequestBody CanalDTO canalDTO)  {
        canalAggService.create(canalDTO);
        return RestResponse.ok();

    }

    /**
     * 修改canal
     */
    @PostMapping("modify")
    public RestResponse<Void> modify(@RequestBody CanalDTO canalDTO) {
        canalAggService.modify(canalDTO);
        return RestResponse.ok();
    }
}
